# Copyright 2023 TikTok Pte. Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exports target DPCA_PSI::dpca_psi
#
# Creates variables:
#   DPCA_PSI_FOUND : If DPCA_PSI was found
#   DPCA_PSI_VERSION : the full version number
#   DPCA_PSI_VERSION_MAJOR : the major version number
#   DPCA_PSI_VERSION_MINOR : the minor version number
#   DPCA_PSI_VERSION_PATCH : the patch version number
#   DPCA_PSI_BUILD_TYPE : The build type (e.g., "Release" or "Debug")

@PACKAGE_INIT@

include(CMakeFindDependencyMacro)

macro(DPCA_PSI_find_dependency dep)
    find_dependency(${dep})
    if(NOT ${dep}_FOUND)
        if(NOT DPCA_PSI_FIND_QUIETLY)
            message(WARNING "Could not find dependency `${dep}` required by this configuration")
        endif()
        set(DPCA_PSI_FOUND FALSE)
        return()
    endif()
endmacro()

set(DPCA_PSI_FOUND FALSE)
set(DPCA_PSI_STATIC_FOUND FALSE)
set(DPCA_PSI_SHARED_FOUND FALSE)

set(DPCA_PSI_VERSION @DPCA_PSI_VERSION@)
set(DPCA_PSI_VERSION_MAJOR @DPCA_PSI_VERSION_MAJOR@)
set(DPCA_PSI_VERSION_MINOR @DPCA_PSI_VERSION_MINOR@)
set(DPCA_PSI_VERSION_PATCH @DPCA_PSI_VERSION_PATCH@)
set(DPCA_PSI_BUILD_TYPE @CMAKE_BUILD_TYPE@)
set(DPCA_PSI_CARRY_GLOG @DPCA_PSI_CARRY_GLOG@)
set(DPCA_PSI_CARRY_OPENSSL @DPCA_PSI_CARRY_OPENSSL@)

DPCA_PSI_find_dependency(nlohmann_json REQUIRED)
DPCA_PSI_find_dependency(IPCL REQUIRED)
DPCA_PSI_find_dependency(OpenMP REQUIRED)

if(NOT DPCA_PSI_CARRY_GLOG)
    DPCA_PSI_find_dependency(glog REQUIRED)
endif()
if(NOT DPCA_PSI_CARRY_OPENSSL)
    DPCA_PSI_find_dependency(OpenSSL REQUIRED)
endif()

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
DPCA_PSI_find_dependency(Threads REQUIRED)

# Add the current directory to the module search path
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})

include(${CMAKE_CURRENT_LIST_DIR}/DPCA_PSITargets.cmake)

if(TARGET DPCA_PSI::dpca_psi)
    set(DPCA_PSI_FOUND TRUE)
    set(DPCA_PSI_STATIC_FOUND TRUE)
endif()

if(TARGET DPCA_PSI::dpca_psi_shared)
    set(DPCA_PSI_FOUND TRUE)
    set(DPCA_PSI_SHARED_FOUND TRUE)
endif()

if(DPCA_PSI_FOUND)
    if(NOT DPCA_PSI_FIND_QUIETLY)
        message(STATUS "DPCA_PSI -> Version ${DPCA_PSI_VERSION} detected")
    endif()
    if(DPCA_PSI_DEBUG AND NOT DPCA_PSI_FIND_QUIETLY)
        message(STATUS "Performance warning: DPCA_PSI compiled in debug mode")
    endif()

    set(DPCA_PSI_TARGETS_AVAILABLE "DPCA_PSI -> Targets available:")
    if(DPCA_PSI_STATIC_FOUND)
        string(APPEND DPCA_PSI_TARGETS_AVAILABLE " DPCA_PSI::dpca_psi")
    endif()
    if(DPCA_PSI_SHARED_FOUND)
        string(APPEND DPCA_PSI_TARGETS_AVAILABLE " DPCA_PSI::dpca_psi_shared")
    endif()
    if(NOT DPCA_PSI_FIND_QUIETLY)
        message(STATUS ${DPCA_PSI_TARGETS_AVAILABLE})
    endif()
else()
    if(NOT DPCA_PSI_FIND_QUIETLY)
        message(STATUS "DPCA_PSI -> NOT FOUND")
    endif()
endif()
